package com.bee.shepherd.service.etl;

import com.alibaba.otter.canal.protocol.FlatMessage;
import com.qunar.finance.nbdata.adapter.api.been.RequestParam;
import com.qunar.finance.nbdata.adapter.api.been.ResponseContent;
import com.qunar.finance.nbdata.adapter.api.enums.ServiceStatusCode;
import com.qunar.finance.nbdata.adapter.api.service.BaseEsOperationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class ESServiceImpl implements IESService<FlatMessage>{

    private static final Logger logger = LoggerFactory.getLogger(ESServiceImpl.class);

    @Resource
    private BaseEsOperationService esOpService;

    @Resource
    private WechatParamBuildService paramBuildService;

    @Override
    public ResponseContent save(FlatMessage message){
        Map<String,String> mapData = message.getData().get(0);
        RequestParam requestParam = paramBuildService.saveParam(message);

        logger.info("保存文档:{}",mapData);

        ResponseContent content = esOpService.index(requestParam);

        return content;
    }

    @Override
    public ResponseContent delete(FlatMessage message) {
        List<Map<String,String>> old = message.getData();
        ResponseContent content = null;
        if(old.size() < 1){
            logger.error("没有要删除的数据.");
            content.setServiceStatusCode(ServiceStatusCode.ERROR);
            content.setMessage("没有要删除的数据.");
            return content;
        }
        RequestParam requestParam = paramBuildService.deleteParam(message);
        content = esOpService.delete(requestParam);

        return content;
    }

    @Override
    public ResponseContent update(FlatMessage message) {
        List<Map<String,String>> old = message.getOld();
        if(old.size() < 1){
            logger.error("没有要更新的数据.");
            throw new RuntimeException("没有要更新的数据.");
        }
        RequestParam requestParam = paramBuildService.updateParam(message);
        ResponseContent content = esOpService.update(requestParam);

        return content;
    }

    @Override
    public ResponseContent query(String id) {
        return null;
    }

    @Override
    public ResponseContent batchSave(List<FlatMessage> list) {
//        List<RequestParam> params = paramBuildService.batchSaveParam(list);
//        esOpService.mutiIndex(params);
        return null;
    }

    @Override
    public ResponseContent batchDelete(List<FlatMessage> list) {
        return null;
    }

    @Override
    public ResponseContent batchUpdate(List<FlatMessage> list) {
        return null;
    }


}
